//
//  SecondViewController.m
//  UI_豆瓣
//
//  Created by dllo on 16/6/25.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "SSecondViewController.h"

#define WIDTH self.view.frame.size.width

#define HEIGHT self.view.frame.size.height

@interface SSecondViewController ()
@property (nonatomic ,retain)NSMutableArray *array;
@property (nonatomic ,retain)UIImageView *imageVView;
@property (nonatomic ,retain)UILabel *actorLabel;
@property (nonatomic ,retain)UILabel *simpleLabel;
@property (nonatomic ,retain)UILabel *rating;
@property (nonatomic ,retain)UILabel *rating_count;
@property (nonatomic ,retain)UILabel *release_date;
@property (nonatomic ,retain)UILabel *runtime;
@property (nonatomic ,retain)UILabel *genres;
@property (nonatomic ,retain)UILabel *film_locations;

@property(nonatomic, retain)UIScrollView *myScrollView;

@end

@implementation SSecondViewController
-(void)dealloc{
    [_array release];
    [_imageVView release];
    [_actorLabel release];
    [_simpleLabel release];
    [_rating release];
    [_genres release];
    [_mytitle release];
    [_rating_count release];
    [_release_date release];
    [_runtime release];
    [_film_locations release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self handleData];
    
    [self config];
    
       
    [self setPropertys]; // 先取出数据, 否则 文字自适应高度为0. 不能先取出文字

    
    
    [self creatButton];

    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)handleData{
    self.array = [NSMutableArray array];
    for (int i = 110; i < 136; i++) {
        
        NSString *path = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"m%d", i] ofType:@"txt"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
      
        NSDictionary *dic = [dictionary valueForKey:@"result"];
    
        if ([[dic objectForKey:@"title"] isEqualToString:self.mytitle]) {
            
            [self.array addObject:dic];
         
            
        }
        
    }

}
-(void)config{
    
    
    // 写 UIScrollView 时 一定要注意 程序 的执行 顺序
    self.myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,WIDTH, HEIGHT )];
//    self.myScrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.myScrollView];
    [_myScrollView release];
    
    
    
    
    
    //滑动页数
    self.myScrollView.contentSize = CGSizeMake(0, self.simpleLabel.frame.size.width + HEIGHT + 4);
    
    

    
    
    NSDictionary *dic = [self.array objectAtIndex:0];
    
    self.navigationItem.title = [dic valueForKey:@"title"]; // 制作人 与 电影情节之间的文字
    self.actorLabel = [[UILabel alloc]init];
    self.actorLabel.text = [dic valueForKey:@"actors"];
    [self.myScrollView addSubview:self.actorLabel];
    [_actorLabel release];
    
    
    self.simpleLabel = [[UILabel alloc]init];
    self.simpleLabel.text = [dic valueForKey:@"plot_simple"]; // 故事介绍
    [self.myScrollView addSubview:_simpleLabel];
  //  self.simpleLabel.backgroundColor = [UIColor cyanColor];
    [_simpleLabel release];
    
    
    self.rating =[[UILabel alloc]init];
    self.rating.text = [dic valueForKey:@"rating"];
    [self.myScrollView addSubview:_rating];
 //   self.rating.backgroundColor = [UIColor blueColor];
    [_rating release];
    
    
    
    self.rating_count = [[UILabel alloc]init];
    self.rating_count.text = [dic valueForKey:@"rating_count"];
    [self.myScrollView addSubview:_rating_count];
    [_rating_count release];
    
    
    self.release_date = [[UILabel alloc]init];
    self.release_date.text = [dic valueForKey:@"release_date"];
    [self.myScrollView addSubview:_release_date];
    [_release_date release];
    
    
    
    self.runtime = [[UILabel alloc]init];
    self.runtime.text = [dic valueForKey:@"runtime"];
    [self.myScrollView addSubview:_runtime];
    
    
    self.genres = [[UILabel alloc]init];
    self.genres.text = [dic valueForKey:@"genres"];
    [self.myScrollView addSubview:_genres];
    [_genres release];
    
    
    self.film_locations = [[UILabel alloc]init];
    self.film_locations.text = [dic valueForKey:@"film_locations"];
    [self.myScrollView addSubview:_film_locations];
    [_film_locations release];
    
    
    NSString *path  = [dic valueForKey:@"poster"];
  
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];
    self.imageVView = [[UIImageView alloc]initWithImage:[UIImage imageWithData:data]];

 [self.myScrollView addSubview:_imageVView];
    
}



-(void)setPropertys{
    
    self.imageVView.frame = CGRectMake(20, 20, WIDTH * 0.3, HEIGHT * 0.3);
    UILabel *labelPF = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH * 0.3 + 50, 20, WIDTH * 0.2, HEIGHT * 0.3 / 5)];
    labelPF.text = @"评分:";
    [self.myScrollView addSubview:labelPF];
    self.rating.frame = CGRectMake(WIDTH * 0.5 + 50, 20, WIDTH * 0.1, HEIGHT * 0.3 / 5);
    self.rating_count.frame = CGRectMake(WIDTH * 0.6 + 50, 20, WIDTH * 0.1, HEIGHT * 0.3 / 5);
    UILabel *labelPL = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH * 0.7 + 50, 20, WIDTH * 0.3  - 70, HEIGHT * 0.3 / 5)];
    labelPL.text = @"评论";
    [self.myScrollView addSubview:labelPL];
    
    self.release_date.frame = CGRectMake(WIDTH * 0.3 + 50, 20 + HEIGHT * 0.3 / 5 , WIDTH * 0.7 - 70, HEIGHT * 0.3 / 5);
    self.runtime.frame = CGRectMake(WIDTH * 0.3 + 50, 20 + 2 * HEIGHT * 0.3 / 5, WIDTH * 0.7 - 70, HEIGHT * 0.3 / 5);
    self.genres.frame = CGRectMake(WIDTH * 0.3 + 50 , 20 + 3 * HEIGHT * 0.3 / 5, WIDTH * 0.7 - 70, HEIGHT * 0.3 / 5);
    self.film_locations.frame = CGRectMake(WIDTH * 0.3 + 50, 20 + 4 * HEIGHT * 0.3 / 5, WIDTH * 0.7 - 70, HEIGHT * 0.3 / 5);
    
    
    
    
    
    
    UILabel *labelZZ = [[UILabel alloc]initWithFrame:CGRectMake(20, HEIGHT * 0.3 + 30, WIDTH - 40, HEIGHT * 0.05)];
    labelZZ.text = @"制作人";
    labelZZ.font = [UIFont systemFontOfSize:25];
    [self.myScrollView addSubview:labelZZ];
    self.actorLabel.frame = CGRectMake(20, HEIGHT * 0.35 + 40, WIDTH - 40, HEIGHT * 0.1);
    self.actorLabel.numberOfLines = 0;
    
    UILabel *labelMovie = [[UILabel alloc]initWithFrame:CGRectMake(20, HEIGHT * 0.45 + 30, WIDTH - 40 , HEIGHT * 0.05)];
    labelMovie.text = @"电影情节";
    labelMovie.font = [UIFont systemFontOfSize:25];
    [self.myScrollView addSubview:labelMovie];
    NSDictionary *attributesdic = @{
                                    NSFontAttributeName :[UIFont systemFontOfSize:17]
                                    
                                    
                                    };
    
    CGRect rect = [self.simpleLabel.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, HEIGHT * 5) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesdic context:nil];

    self.simpleLabel.frame = CGRectMake(20, HEIGHT * 0.45 + 60, WIDTH - 40, rect.size.height ) ;
    self.simpleLabel.font = [UIFont systemFontOfSize:15];
    self.simpleLabel.numberOfLines = 0;

    
    
}

-(void)creatButton{
  
    
    
}
-(void)rightButtonAction:(UIBarButtonItem *)button{
   
    
    
}










@end
